import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

import javax.swing.*;

public class Editor extends JPanel implements MouseListener {
	Image image;
	OpaqueLayer drag;
	ArrayList<String> script = new ArrayList<String>();

	public Editor(String img, OpaqueLayer drag)
	{
		this.drag = drag;
		image = ImageDB.images.get(img);
		while(image.getWidth(null) == -1);
		this.setSize(new Dimension(image.getWidth(null), image.getHeight(null)));
		this.setMinimumSize(new Dimension(image.getWidth(null), image.getHeight(null)));
		this.setPreferredSize(new Dimension(image.getWidth(null), image.getHeight(null)));
		this.setMaximumSize(new Dimension(image.getWidth(null), image.getHeight(null)));    
		this.addMouseListener(this);
		// script = load script
		if (Main.default_bool) {
			try {
	    		BufferedReader in = new BufferedReader(new FileReader(Main.srcPath + "script.rb"));
	    		String str;
	    		while ((str = in.readLine()) != null) {
	    			script.add(str);
	    		}
	    		in.close();
	    	} catch (IOException e) {
	    	}	
		}
	}

	@Override
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g); 
		if (image != null)
			g.drawImage(image, 0,0,this.getWidth(),this.getHeight(),this);
		int x = 10;
		int y = 20;
		for(String s : script) {
			g.drawString(s, x, y);
			y+=g.getFontMetrics().getHeight();
		}
	}

	private int coordToArrayEntry (int my) {
		if (script.size() == 0) {
			return (-1);
		}
		int fontHeight = this.getGraphics().getFontMetrics().getHeight();
		int y = 20;
		int entry = 0;
		int diffy = my - y;
		while (diffy > 0 && entry < script.size() - 1) {
			entry++;
			y += fontHeight;
			diffy = my - y;
		}
		return (entry);
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		if(drag.active) {
			script.add(coordToArrayEntry(arg0.getY()), drag.dragPiece.getText());
			drag.deactivate();
			drag.repaint();
		}
		else {
			int entry = coordToArrayEntry(arg0.getY());
			if (entry == -1)
				return;
			String text = script.get(entry);
			JLabel dragPiece = new JLabel(text);
	        Graphics g = drag.getGraphics();
	        int w = g.getFontMetrics().charsWidth(text.toCharArray(), 0, text.length());
	        int h = g.getFontMetrics().getHeight();
	        Rectangle r = new Rectangle(w + 2, h + 2);
	        dragPiece.setBounds(r);
			drag.activate(dragPiece);
			drag.repaint();
			script.remove(entry);
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
	}
}
